<?php 
	@session_start();
	error_reporting(E_ERROR);  
    ini_set("display_errors","Off");  
	require_once('../user/wxbrower.php');
	require_once("../includes/public.inc.php");
	$s = isset($_GET['s'])?$_GET['s']:'';
	//$username = isset($_SESSION['username'])?$_SESSION['username']:'';
	$sysconfig=mysql_fetch_array(mysql_query("select * from ".DBQIAN."sys_config limit 1"));
	if (!$s)
	{
		echo "该二维码为非法防伪码，购买的产品请谨慎使用!";exit;
		//echo "<script type='text/javascript'>alert('该二维码为非法防伪码，请谨慎使用!');return false;</script>";exit;
	}
	if (!isset($_GET['code']))
	{
		$uwxcode=$_GET['uwxcode'];
		$locaurl=urlencode(WEBNAME."main/zhengpu.php?s=".$s."&uwxcode=".$uwxcode);//网址解析
		$urls="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".trim($sysconfig['cappid'])."&redirect_uri=".$locaurl."&response_type=code&scope=snsapi_base#wechat_redirect";
		header("Location:".$urls);exit;
	}
	$code = $_GET['code'];//获取code
	$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret']."&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
	$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
	$array = get_object_vars($jsondecode);//转换成数组
	$openid = $array['openid'];//输出openid
	
	//获取用户微信openid
	//$openid = '1';	//使用单一入口
	/*function getAccessToken()
	{
		$sysconfig=mysql_fetch_array(mysql_query("select * from ".DBQIAN."sys_config limit 1"));
		 if ($sysconfig['ctokentime'] < time()) {
	      // 如果是企业号用以下URL获取access_token
	      // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
	      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$sysconfig['cappid']."&secret=".$sysconfig['cappsecret'];
	      $res = json_decode(curlGet($url));
	      $access_token = $res->access_token;
	      if ($access_token) {
	        $ctokentime = time() + 7000;
	        $ctoken = $access_token;
			mysql_query("update ".DBQIAN."sys_config set ctoken='$ctoken',
						  ctokentime='$ctokentime', where id=1");
	        //$fp = fopen("access_token.json", "w");
	       // fwrite($fp, json_encode($data));
	        //fclose($fp);
	      }
	    } else {
	      $access_token = $sysconfig['ctoken'];
	    }
	    return $access_token;
	}
	
	function curlGet($url)
	{
		$curl = curl_init();
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	    curl_setopt($curl, CURLOPT_URL, $url);
	
	    $res = curl_exec($curl);
	    curl_close($curl);
	
	    return $res;
	}*/
	
	/*$access_token=getAccessToken();//不需要关注公众号，暂时不需要使用这段代码
	$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid;
	$subscribe = json_decode(curlGet($subscribe_msg),true);
	$zyxx = $subscribe['subscribe'];
	if($zyxx !== 1)
	{
		header("Location: ../user/guanzhu.php");exit;
	}*/
	
	//判断是否关注
	//require_once("../includes/conn.php");
	//$s=isset($_GET['s'])?$_GET['s']:'';
	//$openid = isset($_GET['openid'])?$_GET['openid']:'';//echo $openid;exit;
	//$username = isset($_SESSION['username'])?$_SESSION['username']:'';
	//不需要判断是否关注，主需要判断这个吗是否是存在而且是没被领取的
	//print_r($_SESSION['username']);exit;
	if ($openid)
	{
		//$exsit_openid = mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where username='".trim($_SESSION['username'])."' order by id desc limit 1"));//不需要登录去掉这个
		$fwm = mysql_fetch_array(mysql_query("select id,fwm,time from ".DBQIAN."fwm where fwm='".trim($s)."' and userstatus=0 order by id desc limit 1"));//未使用,未领取奖品的
		$fwm1 = mysql_fetch_array(mysql_query("select id,fwm,time from ".DBQIAN."fwm where fwm='".trim($s)."' and userstatus=1 order by id desc limit 1"));//已经使用,领取奖品的
		//mysql_query("select id,fwm,time from ".DBQIAN."fwm where fwm='".trim($s)."' and userstatus=1 order by id desc limit 1");
		mysql_query("update ".DBQIAN."fwm set times=times+1 where fwm='".trim($s)."'"); //增加次数
	}
	else 
	{
		echo "<script>alert('非法进入，请检查提交的数据是否正确!!');</script>";exit;
	}
?>
<!DOCTYPE html>
<html>
<head lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>卡玫尔</title>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="../images/147/reset.css">
<link rel="stylesheet" href="../images/147/swipe.css">
<link rel="stylesheet" href="../images/147/layer.css">
<link rel="stylesheet" href="../images/147/default.css">
<!-- <link rel="stylesheet" href="/qiaqia/Public/index/css/quick.css" /> 无图模式-->
<script src="../images/147/zepto.min.js"></script>
<script src="../images/147/layer.m.js"></script>
<link href="../images/147/layer(1).css" type="text/css" rel="styleSheet" id="layermcss">
<script src="../images/147/swipe.js"></script>
<script src="../images/147/common.js"></script>
<script></script>
<script type="text/javascript">
	$(function(){
		$("#submit").click(function(){
			var phone = $("#phone").val();
			if (!phone)
			{
				alert('请正确填写手机号码！');
				return false;
			}
		});
	});	
</script>
</head>
<body class="body-color">
	<div class="m-wraper">
	<div class="banner">
		<h1></h1>
		<p>滴滴甘醇，品质流金。</p>
		<!--<img src="/qiaqia/uploads/products/57f88c1c3834e.jpg" alt="" />--> 
		<img src="../images/147/20161013150742.jpg" alt="">
	</div>
	<link rel="stylesheet" href="../images/147/fwsy.css">
	<script src="../images/147/fwsy.js"></script>
	<?if ($openid&&isset($fwm)&&$fwm):?>
	<div class="dis-box">
		<div class="product">
		<form method="get" action="<?=WEBNAME?>main/lj.php">
			<br>
			<p><b>码上扫红包，攒点私房钱</b></p>
			<p style="margin-top:6px;width:100%;line-height:33px;">恭喜你获得由上海卡玫尔新材料科技有限公司提供的随机微信现金红包，参与即中兑奖！</p>
			<p style="margin-top:10px;"><img src="../images/147/detailbtn.png" alt=""></p>
			<br>
			<p style="width:100%;border-top:2px solid orange;"></p>
			<p style="width:100%;">
				<input type="text" name="phone" id="phone" style="width:100%;height:50px;text-align:center;font-size:25px;" placeholder="请输入领奖手机号码" />
			</p>
			<p style="width:100%;border-top:2px solid orange;"></p>
			<br>
			<br>
			<p>
				<input type="hidden" name="s" value="<?=$fwm['fwm']?>" />
				<input type="hidden" name="type" value="user" />
				<input type="hidden" name="openid" value="<?=$openid?>" />
				<!-- <input type="image" src="../images/147/btn.png" value="我要领奖" style="width:100%;color:#fff;border-radius:6px;height:80px;background-color:orange;border:0px;font-weight:bold;font-size:32px;font-family:'微软雅黑';" />-->
				<input type="submit" id="submit" value="" style="width:100%;background-image:url(../images/147/btn.png); background-repeat:no-repeat;background-position:center;background-color:#fff;border-radius:6px;height:80px;background-color:orange;border:0px;font-weight:bold;font-size:32px;font-family:'微软雅黑';" />
			</p>
			<br>
		</form>
		</div>
			
	</div>
	<?elseif ($openid&&isset($fwm1)&&$fwm1):?>
	<div class="dis-box ">
	    <div class="product">
			<p>该码已被扫过<?=$fwm1['times']?>次，奖品是价值<b><?=$fwm1['usercredit']?>元的微信红包</b>，已于<span><?=$fwm1['user_time']?></span>被领走了。</p>
			<div class="product-img"><img src="../images/147/5769fd82224aa.jpg" alt=""></div>
		</div>
		<div class="complain">
			<p>
				<a href="tel:400-098-8852" class="btn-complain">我要投诉</a>
				我的奖品被领走了？<br>
				投诉电话：<br>
				400-098-8852    
			</p>
		</div>
	</div>
	<?else :?>
		<?
			header("Location: nowin.php?s=".$s."&openid=".$openid);exit;
		?>
	<?endif;?>
	<script type="text/javascript">
		$('.mark_').delegate('a#fwsy', 'click', function(){
		    layer.open({
		        type: 1,
		        content: $("#origin_int_box").html(),
		        style: 'width:96%;margin:0 auto;border-radius:3px;'
		    });
		    slider();
		});
	</script>
	<div class="bottom_ad" style="width:100%;clear:both;">
		<h2></h2>
		<img src="../images/147/banner-1.jpg" width="100%" alt=""></div>
	</div>
	<div class="footer">
		<p>©2016 上海卡玫尔新材料科技有限公司兑奖</p>
	</div>
	<div class="mask hide" id="fwin_mask"></div>
	<div id="swait" class="hide">请稍等,系统努力处理中...</div>
</body>
</html>